package com.normalClasses.eye;

import java.util.ArrayList;
import java.util.LinkedList;

public class CookieBehavior {
	 private ArrayList<Long> timeList;
	  //private ArrayList<String> informationList;
	  private final String[] symbols={"visited judge_cookie_status","timing message prompt function activated"};
	  private ArrayList<userBehavior> cookieUserBehaviors;
	  private double threshold=20;
	  //private double upThreshold;
	  //private double downThreshold;
	  
	  public CookieBehavior(){
	    this.timeList=new ArrayList<Long>();
	    //this.informationList=new ArrayList<String>();
	    this.cookieUserBehaviors=new ArrayList<userBehavior>();
	  }
	  public CookieBehavior(LinkedList<userBehavior> userBehaviors){
	    this();
	    this.updateLoginStatus(userBehaviors);
	    }
	  
	 public CookieBehavior(LinkedList<userBehavior> userBehaviors,double threshold) {
	   this();
	   this.setThreshold(threshold);
	   this.updateLoginStatus(userBehaviors);
	   }
	 
	  public ArrayList<Long> getTimeList() {
	    return this.timeList;
	  }
	  
	  public void setThreshold(double threshold) {
	    this.threshold=threshold;
	  }
	  
	  //public ArrayList<String> getInformationList(){
	    //return this.informationList;
	  //}
	  //过滤出跟刷新行为相关的log
	  public void updateLoginStatus(LinkedList<userBehavior> userBehaviors){
	    this.timeList=new ArrayList<Long>();
	    this.cookieUserBehaviors=new ArrayList<userBehavior>();
	    for(int i=0;i<userBehaviors.size();i++){
	      String information=userBehaviors.get(i).getInformation();
	      for(int j=0;j<this.symbols.length;j++){
	        if(information.contains(this.symbols[j])){
	          //informationList.add(information);
	          cookieUserBehaviors.add(userBehaviors.get(i));
	          timeList.add(Long.parseLong((userBehaviors.get(i).getTimeStamp())));
	          break;
	        }
	      }
	    }
	  }
	  
	  private long getMin(){
	    long min=timeList.get(0);
	    for(int i=1;i<this.timeList.size();i++) {
	      if (this.timeList.get(i)<min) {
	        min=timeList.get(i);
	        }
	    }
	    return min;
	  }
	  
	  private long getMax(){
	    Long max=timeList.get(0);
	    for(int i=1;i<this.timeList.size();i++) {
	      if (this.timeList.get(i)>max) {
	        max=timeList.get(i);
	        }
	    }
	    return max;
	  }
	  //行为分析
	  /**
	   * 
	   * @param seconds 规定的秒数，比如输入300秒，意味着当用户在300秒内出现与刷新相关的行为就会被判定为异常
	   * @return 如果返回不是空字符串，那么就是有异常，如果返回是空，则没有异常
	   * @throws java.text.ParseException
	   */
	  public String behaviorAnalysis(int seconds) throws java.text.ParseException{
	    String information="";
	    if(this.timeList.size()>0){
	    int count=0;
	    long start=this.getMin();
	    //System.out.println(start);
	    long end=this.getMax();
	    //System.out.println(end);
	    for(long i=start;i<end;i=i+1000*seconds){
	      for(int j=0;j<this.cookieUserBehaviors.size();j++){
	        if(Long.parseLong(this.cookieUserBehaviors.get(j).getTimeStamp())>=i&&Long.parseLong(this.cookieUserBehaviors.get(j).getTimeStamp())<=i+1000*seconds){
	          //System.out.println("加入");
	          //System.out.println(loginUserBehaviors.get(j).getInformation()+dateToStamp.stampTDate(loginUserBehaviors.get(j).getTimeStamp()));
	          count=count+1;
	        }
	     } 
	      //int frequency=count/seconds;
	      if(count>threshold){
	        information=information+"The user has unusual refresh behavior between "+dateToStamp.stampTDate(String.valueOf(i))+" and "+dateToStamp.stampTDate(String.valueOf(i+1000*seconds))+"\n";
	        //System.out.println("This user has unusual login behavior between"+dateToStamp.stampTDate(String.valueOf(i))+" and "+String.valueOf(i+1000*seconds));
	      }
	      count =0;
	    }
	    return information;
	    }
	    else{ 
	      return information;
	      }
	  }  
}
